package com.tetris.block;

import com.tetris.rule.GameRules;

import java.awt.Color;
import java.util.Random;

/**
 * 俄罗斯方块的具体类型
 */
public class Tetromino extends Block {
    // 方块类型枚举
    public enum Type {
        I(new int[][]{{1, 1, 1, 1}}, Color.CYAN),
        O(new int[][]{{1, 1}, {1, 1}}, Color.YELLOW),
        T(new int[][]{{0, 1, 0}, {1, 1, 1}}, Color.MAGENTA),
        J(new int[][]{{1, 0, 0}, {1, 1, 1}}, Color.BLUE),
        L(new int[][]{{0, 0, 1}, {1, 1, 1}}, Color.ORANGE),
        S(new int[][]{{0, 1, 1}, {1, 1, 0}}, Color.GREEN),
        Z(new int[][]{{1, 1, 0}, {0, 1, 1}}, Color.RED);

        private final int[][] shape;
        private final Color color;

        Type(int[][] shape, Color color) {
            this.shape = shape;
            this.color = color;
        }

        public int[][] getShape() {
            return shape;
        }

        public Color getColor() {
            return color;
        }
    }

    private Type type;

    public Tetromino(int x, int y, Type type) {
        super(x, y);
        this.type = type;
        // 在子类构造函数中调用初始化方法，确保type已赋值
        initialize();
    }

    // 随机生成一个方块
    public static Tetromino getRandomBlock(int x, int y) {
        Random random = new Random();
        Type[] types = Type.values();
        Type randomType = types[random.nextInt(types.length)];
        return new Tetromino(x, y, randomType);
    }

    @Override
    protected void initializeShape() {
        shape = type.getShape();
    }

    @Override
    protected void setColor() {
        color = type.getColor();
    }

    // 获取方块类型
    public Type getType() {
        return type;
    }
}
    